/**
 * Created on 2007-04-22 12:31
 */
package game.media.gfx;

import java.awt.Color;

/**
 * @author David Wallin
 * 
 * The highlighted block state
 */
public class BlockStateHighlighted extends BlockState
{
	/**
	 * The current mutation of the color
	 */
	private int				m_ColorMutator;
	/**
	 * The current interval, deciding how m_ColorMutator will be changed
	 * Should always be either m_ColorMutatorInterval, or -m_ColorMutatorInterval
	 */
	private int				m_CurrentColorMutatorInterval;
	/**
	 * Counts the frames
	 */
	private int				m_FrameCounter;
	/**
	 * The standard interval of change for m_ColorMutator
	 */
	private final int		m_ColorMutatorInterval = 0x111111;
	/**
	 * When m_ColorMutator hits this value, m_CurrentColorMutatorInterval is reversed
	 */
	private int		m_UpperBorder = 0x888888;
	/**
	 * When m_ColorMutator hits this value, m_CurrentColorMutatorInterval is reversed
	 */
	private int		m_LowerBorder = 0x0;
	/**
	 * The number of frames between changes in m_ColorMutator
	 */
	private final int		m_ChangeColorMutatorInterval = 1;
	private Color			m_Color;
	
	/**
	 * Constructor, calls super, initializes members
	 */
	public BlockStateHighlighted(Entity entity, Color color)
	{
		super(entity);
		
		m_ColorMutator = 0x0;
		m_CurrentColorMutatorInterval = m_ColorMutatorInterval;
		m_FrameCounter = 0;
		m_Color = color;
		
		if(m_Color.getRed() == 0xFF)
		{
			m_CurrentColorMutatorInterval -= 0x110000;
			m_UpperBorder -= 0x880000;
		}
		
		if(m_Color.getGreen() == 0xFF)
		{
			m_CurrentColorMutatorInterval -= 0x1100;
			m_UpperBorder -= 0x8800;
		}
		
		if(m_Color.getBlue() == 0xFF)
		{
			m_CurrentColorMutatorInterval -= 0x11;
			m_UpperBorder -= 0x88;
		}
	}
	
	/* (non-Javadoc)
	 * @see game.BlockState#draw(java.awt.Color)
	 */
	@Override
	public void draw(Color color)
	{
		m_FrameCounter++;
		
		if(m_FrameCounter > m_ChangeColorMutatorInterval)
		{
			m_ColorMutator += m_CurrentColorMutatorInterval;
		}
		
		int colorValue = m_Color.getRGB();
		colorValue += m_ColorMutator;
		
		if(m_FrameCounter > m_ChangeColorMutatorInterval)
		{
			if((m_ColorMutator >= m_UpperBorder) || (m_ColorMutator <= m_LowerBorder))
			{
				m_CurrentColorMutatorInterval = -m_CurrentColorMutatorInterval;
			}
			
			m_FrameCounter = 0;
		}
		
		Color mutatedColor = Color.getColor("", colorValue);
		m_RenderManager.fillRect(m_Entity.getXpos(), m_Entity.getYpos(), m_Entity.getWidth(), m_Entity.getHeight(), mutatedColor);
	}
	/*
	private String decToHex(int n)
	{
		String hex = "";
		int devisor = 1;
		
		while((n / (devisor * 16)) > 0)
		{
			devisor *= 16;
		}
		
		while((devisor / 16) > 0)
		{
			hex += decToHexOneDigit(n / devisor);
			n %= devisor;
			devisor /= 16;
		}
		
		hex += String.valueOf(n);
		
		while(hex.length() < 8)
		{
			hex = "0" + hex;
		}
		
		hex = "0x" + hex;
		
		return hex;
	}
	
	private String decToHexOneDigit(int n) throws IllegalArgumentException
	{
		if((n < 0) || (n > 15))
		{
			throw new IllegalArgumentException("Minimum 0, maximum 15, was : " + n);
		}
		
		switch(n)
		{
			case 10: return "A";
			case 11: return "B";
			case 12: return "C";
			case 13: return "D";
			case 14: return "E";
			case 15: return "F";
			default: return String.valueOf(n);
		}
	}
	*/
}
